package com.seckill.Service.Impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.seckill.Cache.GoodsSecKillCache;
import com.seckill.Common.BaseResponse;
import com.seckill.Common.CustomException;
import com.seckill.Mapper.GoodsSecKillMapper;
import com.seckill.Model.DO.GoodsSeckill;
import com.seckill.Service.GoodsSecKillService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.Objects;

@Service
@Slf4j
public class GoodsSecKillServiceImpl implements GoodsSecKillService {

    @Autowired
    private GoodsSecKillMapper goodsSecKillMapper;

    @Autowired
    private GoodsSecKillCache goodsSecKillCache;

    @Override
    public Object createSnapped(GoodsSeckill goodsSeckill) {
        //校验参数
        if(goodsSeckill.getGoodsId() == null){
            throw new CustomException("创建秒杀任务出现异常，商品为空");
        }
        if (null == goodsSeckill.getStockCount() || goodsSeckill.getStockCount() <= 0){
            throw new CustomException("库存不得为空");
        }
        //校验开始和结束时间
        if (null == goodsSeckill.getStartDate() || null == goodsSeckill.getEndDate()){
            //默认开始时间为2分钟后，持续时间为10分钟
            Calendar calendar = Calendar.getInstance(); //获取当前时间
            calendar.add(Calendar.MINUTE, 2);
            goodsSeckill.setStartDate(calendar.getTime());
            calendar.add(Calendar.MINUTE, 10);
            goodsSeckill.setEndDate(calendar.getTime());
        }
        if (goodsSeckill.getStartDate().before(new Date())){
            throw new CustomException("开始时间不能小于当前时间");
        }
        try {
            //将秒杀任务存入数据库
            goodsSecKillMapper.insert(goodsSeckill);
            //将秒杀任务放入缓存
            goodsSecKillCache.addSecKill(goodsSeckill);
        }catch (Exception e){
            log.error("创建秒杀任务出现异常 ： {}" + e.getMessage());
            e.printStackTrace();
            throw new CustomException("创建秒杀任务失败，请联系管理员");
        }
        //进行数据预热
        goodsSecKillCache.addToken(goodsSeckill);

        return BaseResponse.success("秒杀任务创建成功");
    }

    @Override
    public BaseResponse getSnappedList(Integer pageNo, Integer pageSize) {
        IPage<GoodsSeckill> goodsSeckillList = goodsSecKillCache.getSecKillList(pageNo,pageSize);
        if(!Objects.isNull(goodsSeckillList)){
            return BaseResponse.success(goodsSeckillList);
        }

        IPage<GoodsSeckill> goodsSeckillIPage = new Page<>(pageNo,pageSize);
        goodsSeckillList = goodsSecKillMapper.selectPage(goodsSeckillIPage, null);
        goodsSecKillCache.addSecKillList(goodsSeckillList,pageNo,pageSize);
        return BaseResponse.success(goodsSeckillList);
    }

    @Override
    public BaseResponse getSnappedById(Long id) {
        GoodsSeckill goodsSeckill = goodsSecKillCache.getSecKill(id);
        if(Objects.isNull(goodsSeckill)){
            goodsSeckill = goodsSecKillMapper.selectById(id);
            if(null == goodsSeckill){
                throw new CustomException("秒杀活动不存在！！！");
            }
            goodsSecKillCache.addSecKill(goodsSeckill);
        }
        return BaseResponse.success(goodsSeckill);
    }
}
